package wondersgroup.hsswidget.scroll;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import wondersgroup.hsswidget.R;

/**
 * <b>Project:</b> MyView<br>
 * <b>Create Date:</b> 2016/10/20 0020<br>
 * <b>Author:</b> huang saisai <br>
 * <b>Description:</b> <br>
 */
public class DiscrollViewGroup extends FrameLayout implements  DiscrollInterface{
   boolean mDiscrollAlpha, mDiscrollScaleX, mDiscrollScaleY;
    int mWidth, mHeight;

    boolean fromBottom, fromTop,fromLeft,fromRight;
    final int ORIENTION_FROM_BOTTOM = 0x01;
    final int ORIENTION_FROM_TOP = 0x02;
    final int ORIENTION_FROM_LEFT = 0x04;
    final int ORIENTION_FROM_RIGHT = 0x08;


    public DiscrollViewGroup(Context context) {
        super(context);
    }

    public DiscrollViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DiscrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DiscrollViewGroup, defStyleAttr, 0);
        mDiscrollAlpha = ta.getBoolean(R.styleable.DiscrollViewGroup_alpha,false);
        mDiscrollScaleX = ta.getBoolean(R.styleable.DiscrollViewGroup_scaleX,false);
        mDiscrollScaleY = ta.getBoolean(R.styleable.DiscrollViewGroup_scaleY,false);
        int from = ta.getInteger(R.styleable.DiscrollViewGroup_translate,-1);
        fromBottom = isDiscrollTranslationFrom(ORIENTION_FROM_BOTTOM, from);
        fromTop = isDiscrollTranslationFrom(ORIENTION_FROM_TOP, from);
        fromLeft = isDiscrollTranslationFrom(ORIENTION_FROM_LEFT, from);
        fromRight = isDiscrollTranslationFrom(ORIENTION_FROM_RIGHT, from);

        ta.recycle();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;
    }

    @Override
    public void onDiscroll(float ratio) {
        if (mDiscrollAlpha){
            setAlpha(ratio);
        }
        if (mDiscrollScaleX){
            setScaleX(ratio);
        }
        if (mDiscrollScaleY){
            setScaleY(ratio);

        }
        if (fromBottom){
            setTranslationY(mHeight * (1-ratio));//0 是归为
        }

        if (fromTop){
            setTranslationY(-mHeight * (1-ratio));//0 是归为

        }

        if (fromLeft){
            setTranslationX(-mWidth*(1-ratio));
        }
        if (fromRight){
            setTranslationX(mWidth*(1-ratio));

        }
    }

    boolean isDiscrollTranslationFrom( int desValue, int value){
        return ((desValue & value )== desValue) ? true: false;
    }

    public void onRestScroll(){
        if (mDiscrollAlpha){
            setAlpha(0);
        }
        if (mDiscrollScaleX){
            setScaleX(0);
        }
        if (mDiscrollScaleY){
            setScaleY(0);

        }
        if (fromBottom){
            setTranslationY(mHeight);//0 是归为
        }

        if (fromTop){
            setTranslationY(-mHeight );//0 是归为

        }

        if (fromLeft){
            setTranslationX(-mWidth);
        }
        if (fromRight){
            setTranslationX(mWidth);

        }
    }

    public void setmDiscrollAlpha(boolean mDiscrollAlpha) {
        this.mDiscrollAlpha = mDiscrollAlpha;
    }

    public void setmDiscrollScaleX(boolean mDiscrollScaleX) {
        this.mDiscrollScaleX = mDiscrollScaleX;
    }

    public void setmDiscrollScaleY(boolean mDiscrollScaleY) {
        this.mDiscrollScaleY = mDiscrollScaleY;
    }

    public void setFromBottom(boolean fromBottom) {
        this.fromBottom = fromBottom;
    }

    public void setFromLeft(boolean fromLeft) {
        this.fromLeft = fromLeft;
    }

    public void setFromTop(boolean fromTop) {
        this.fromTop = fromTop;
    }

    public void setFromRight(boolean fromRight) {
        this.fromRight = fromRight;
    }



}
